from django.shortcuts import render,HttpResponse,redirect
from django.contrib.auth.models import User
from django.contrib.auth import authenticate,login,logout
# Create your views here.
def index(request):
    return HttpResponse('ok')

def register(request):
    if request.method == 'GET':
        return render(request,'register.html')
    if request.method == 'POST':
        name = request.POST.get('name','')
        password = request.POST.get('password','')

        # User.objects.create_superuser(username=name,password=password)
        User.objects.create_user(username=name,password=password)
        return redirect('app1:index')

def loginx(request):
    if request.method =='GET':
        return render(request,'login.html')
    if request.method == 'POST':
        name = request.POST.get('name','')
        password = request.POST.get('password','')

        user = authenticate(username=name,password=password)
        if user:
            login(request,user=user)
            return HttpResponse('登陆成功')
        return HttpResponse('登录失败')